﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Thor.Common;
using Thor.WebRole;
using Thor.WebRole.Services;

namespace Thor.Silverlight.Views
{
    public partial class Settings : Page
    {
        private ProviderManagerDomainContext _providerManagerDomainContext = null;
        private TimeZoneDomainContext _timeZoneDomainContext = null;

        private string _providerType = string.Empty;

        public Settings()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void dataForm1_ContentLoaded(object sender, DataFormContentLoadEventArgs e)
        {
            LoadTimeZones();
            LoadProviderAssemblies();
        }

        private void dataForm1_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            if (this.domainDataSource1.HasChanges)
                this.domainDataSource1.SubmitChanges();
        }

        private void ProviderHyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Zip Files (.zip)|*.zip";

            bool? ok = openFileDialog.ShowDialog();

            if (ok == true)
            {
                FileStream stream = openFileDialog.File.OpenRead();

                byte[] bytes = new byte[stream.Length];
                int numBytesToRead = (int)stream.Length;
                int numBytesRead = 0;

                stream.Read(bytes, numBytesRead, numBytesToRead);

                ProviderInfo providerInfo = new ProviderInfo();
                providerInfo.Name = openFileDialog.File.Name;
                providerInfo.Bytes = bytes;

                stream.Close();

                ProviderManagerDomainContext context = new ProviderManagerDomainContext();
                context.ProviderInfos.Add(providerInfo);
                context.SubmitChanges().Completed += new EventHandler(ProviderManagerDomainContext_SubmitChangedCompleted);
            }
        }

        private void ProviderManagerDomainContext_SubmitChangedCompleted(object sender, EventArgs e)
        {
            LoadProviderAssemblies();
        }

        private void ProviderManagerDomainContext_LoadCompleted(object sender, EventArgs e)
        {
            ComboBox providerAssembliesComboBox = GetComboBoxInDataForm("ProviderAssembliesComboBox");
            Thor.Domain.Configuration.Settings settings = (Thor.Domain.Configuration.Settings)this.dataForm1.CurrentItem;

            if (providerAssembliesComboBox != null && settings != null)
            {
                if (providerAssembliesComboBox.ItemsSource != null
                    && providerAssembliesComboBox.ItemsSource is System.Windows.Ria.EntitySet<Thor.Common.ProviderAssembly>)
                {
                    System.Windows.Ria.EntitySet<Thor.Common.ProviderAssembly> entitySet = (System.Windows.Ria.EntitySet<Thor.Common.ProviderAssembly>)providerAssembliesComboBox.ItemsSource;
                    var selectedItem = (from entity in entitySet
                             where entity.Name == settings.AssemblyName
                             select entity).FirstOrDefault();

                    if (selectedItem != null)
                        providerAssembliesComboBox.SelectedItem = selectedItem;
                }
            }
        }

        private void TimeZoneDomainContext_Completed(object sender, EventArgs e)
        {
            ComboBox serverTimeZonesComboBox = GetComboBoxInDataForm("ServerTimeZonesComboBox");
            Thor.Domain.Configuration.Settings settings = (Thor.Domain.Configuration.Settings)this.dataForm1.CurrentItem;

            if (serverTimeZonesComboBox != null && settings != null)
            {
                if (serverTimeZonesComboBox.ItemsSource != null
                    && serverTimeZonesComboBox.ItemsSource is System.Windows.Ria.EntitySet<Thor.Common.TimeZone>)
                {
                    System.Windows.Ria.EntitySet<Thor.Common.TimeZone> entitySet = (System.Windows.Ria.EntitySet<Thor.Common.TimeZone>)serverTimeZonesComboBox.ItemsSource;
                    var selectedItem = (from entity in entitySet
                                        where entity.Name == settings.ServerTimeZone
                                        select entity).FirstOrDefault();

                    if (selectedItem != null)
                        serverTimeZonesComboBox.SelectedItem = selectedItem;
                }
            }
        }

        private void ProviderAssembliesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadProviderTypes();
        }

        private void LoadProviderAssemblies()
        {
            _providerManagerDomainContext = new ProviderManagerDomainContext();
            GetComboBoxInDataForm("ProviderAssembliesComboBox").ItemsSource = _providerManagerDomainContext.ProviderAssemblies;
            _providerManagerDomainContext.Load(_providerManagerDomainContext.GetAssembliesQuery()).Completed += new EventHandler(ProviderManagerDomainContext_LoadCompleted);
        }

        private void LoadTimeZones()
        {
            _timeZoneDomainContext = new TimeZoneDomainContext();
            GetComboBoxInDataForm("ServerTimeZonesComboBox").ItemsSource = _timeZoneDomainContext.TimeZones;
            _timeZoneDomainContext.Load(_timeZoneDomainContext.GetTimeZonesQuery()).Completed += new EventHandler(TimeZoneDomainContext_Completed);
        }

        private void LoadProviderTypes()
        {
            ComboBox providerAssembliesComboBox = GetComboBoxInDataForm("ProviderAssembliesComboBox");
            
            if (providerAssembliesComboBox != null)
            {
                if (providerAssembliesComboBox.SelectedItem != null && _providerManagerDomainContext !=null)
                {
                    var assembly = (from providerAssembly in _providerManagerDomainContext.ProviderAssemblies
                                    where providerAssembly.Name == providerAssembliesComboBox.SelectedItem.ToString()
                                    select providerAssembly).FirstOrDefault();

                    if (assembly != null)
                    {
                        GetComboBoxInDataForm("ProviderTypesComboBox").ItemsSource = assembly.Types;

                        Thor.Domain.Configuration.Settings settings = (Thor.Domain.Configuration.Settings)this.dataForm1.CurrentItem;
                        if (settings != null)
                        {
                            var selectedItem = (from type in assembly.Types
                                                where type == settings.AssemblyType
                                                select type).FirstOrDefault();

                            if (selectedItem != null)
                                GetComboBoxInDataForm("ProviderTypesComboBox").SelectedItem = selectedItem;
                        }
                    }
                }
            }
        }

        private ComboBox GetComboBoxInDataForm(string name)
        {
            return (ComboBox)dataForm1.FindNameInContent(name);
        }
    }
}
